package com.bookstore.back.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bookstore.back.entity.BookCategory;
import com.bookstore.back.mapper.BookCategoryMapper;
import com.bookstore.back.service.CategoryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 图书分类服务实现类
 * 
 * @author 程序猿_Ti
 * @since 2025-07-19
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<BookCategoryMapper, BookCategory> implements CategoryService {

    @Override
    public IPage<BookCategory> getCategoryList(Map<String, Object> params) {
        // 分页参数
        int current = Integer.parseInt(params.getOrDefault("current", "1").toString());
        int size = Integer.parseInt(params.getOrDefault("size", "10").toString());
        
        // 查询条件
        QueryWrapper<BookCategory> queryWrapper = new QueryWrapper<>();
        
        // 名称搜索
        String name = (String) params.get("name");
        if (StringUtils.isNotBlank(name)) {
            queryWrapper.like("name", name);
        }
        
        // 状态筛选
        String statusStr = (String) params.get("status");
        if (StringUtils.isNotBlank(statusStr)) {
            Integer status = Integer.parseInt(statusStr);
            queryWrapper.eq("status", status);
        }

        // 父分类筛选
        String parentIdStr = (String) params.get("parentId");
        if (StringUtils.isNotBlank(parentIdStr)) {
            Integer parentId = Integer.parseInt(parentIdStr);
            queryWrapper.eq("parent_id", parentId);
        }
        
        // 按排序值和创建时间排序
        queryWrapper.orderByAsc("sort_order").orderByAsc("created_at");
        
        return this.page(new Page<>(current, size), queryWrapper);
    }

    @Override
    public List<BookCategory> getCategoryTree() {
        QueryWrapper<BookCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1) // 只查询启用的分类
                   .orderByAsc("sort_order")
                   .orderByAsc("created_at");
        
        return this.list(queryWrapper);
    }

    @Override
    public boolean createCategory(BookCategory category) {
        // 设置默认值
        if (category.getStatus() == null) {
            category.setStatus(1); // 默认启用
        }
        if (category.getSortOrder() == null) {
            category.setSortOrder(0);
        }
        if (category.getParentId() == null) {
            category.setParentId(0); // 默认为顶级分类
        }
        
        return this.save(category);
    }

    @Override
    public boolean updateCategory(Integer categoryId, BookCategory category) {
        category.setId(categoryId);
        return this.updateById(category);
    }

    @Override
    public boolean deleteCategory(Integer categoryId) {
        // 检查是否有子分类
        QueryWrapper<BookCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", categoryId);
        long childCount = this.count(queryWrapper);
        
        if (childCount > 0) {
            throw new RuntimeException("该分类下还有子分类，无法删除");
        }
        
        return this.removeById(categoryId);
    }

    @Override
    public boolean toggleCategoryStatus(Integer categoryId, Integer status) {
        BookCategory category = new BookCategory();
        category.setId(categoryId);
        category.setStatus(status);
        return this.updateById(category);
    }

}
